﻿using CastingWellEquipmentGUI.Model;
using CastingWellEquipmentGUI.Utils;
using CastingWellEquipmentGUI.Utils.GuoMi;
using CastingWellEquipmentGUI.ViewModel.Common.Command;
using CastingWellEquipmentGUI.ViewModel.Common.LogTool;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CastingWellEquipmentGUI.ViewModel.Modal.Exit
{
    public class ExitPanelPanelViewModel : ViewModelBase
    {
        /// <summary>
        /// 关闭当前窗体命令
        /// </summary>
        public DelegateCommand CloseAppCommand { get; set; }
        /// <summary>
        /// 退出应用程序命令
        /// </summary>
        public DelegateCommand ExitApplicationCommand { get; set; }

        private string _ErrorInfo;

        public string ErrorInfo
        {
            get { return _ErrorInfo; }
            set
            {
                _ErrorInfo = value;
                RaisePropertyChanged("ErrorInfo");
            }
        }

        private string _ExitPassword;
        public string ExitPassword
        {
            get { return _ExitPassword; }
            set
            {
                _ExitPassword = value;
                RaisePropertyChanged("ExitPassword");
            }
        }


        public ExitPanelPanelViewModel()
        {
            //初始化关闭命令
            CloseAppCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(CloseAppHandler)
            };

            ExitApplicationCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(ExitApplicationHandler)
            };
            ExitPassword = "";
        }

        /// <summary>
        /// 退出程序命令处理程序
        /// </summary>
        /// <param name="obj">参数</param>
        private void CloseAppHandler(object obj)
        {
            ExitPassword = "";
            Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, "HideExitPanel"));
        }

        private void ExitApplicationHandler(object obj)
        {
            try
            {
                if (ExitPassword == "")
                {
                    ErrorInfo = "请输入密码";
                    return;
                }
                if (LogTool.CurrentUser.Password == new Sm4Crypto().Encrypt_ECB_Str(ExitPassword))
                {
                    ErrorInfo = "";
                    Environment.Exit(0);
                }
                else
                {
                    ErrorInfo = "密码错误";
                    return;
                }
            }
            catch (Exception e)
            {
                LogTool.SaveSysLog(e.Message, "退出异常");
            }
        }
    }
}
